package com.example.sys.job.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.security.annotation.AuthMethod;
import com.example.core.controller.BaseController;
import com.example.core.utils.Result;
import com.example.core.utils.StringUtils;
import com.example.sys.job.po.SysJob;
import com.example.sys.job.service.SysJobService;
import com.example.sys.job.vo.SysJobVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("sysJob")
@RestController
public class SysJobController extends BaseController<SysJobService, SysJobVo> {

    @AuthMethod(expression = "SYS_JOB_MANAGE")
    @PostMapping("stopById")
    public Result stopById(@RequestParam Long id) {
        try {
            SysJob sysJob = this.getService().getById(id);
            this.getService().pauseJob(sysJob);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "SYS_JOB_MANAGE")
    @PostMapping("resumeById")
    public Result resumeById(@RequestParam Long id) {
        try {
            SysJob sysJob = this.getService().getById(id);
            this.getService().resumeJob(sysJob);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "SYS_JOB_MANAGE")
    @PostMapping("modifyJob")
    public Result modifyJob(SysJobVo sysJobVo) {
        try {
            this.getService().modifyJob(sysJobVo);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @AuthMethod(expression = "SYS_JOB_MANAGE")
    @PostMapping("runOnce")
    public Result runOnce(@RequestParam Long id) {
        try {
            this.getService().runOnce(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @Override
    public QueryWrapper changeFindPageQueryWrapper(SysJobVo vo) {
        QueryWrapper queryWrapper = super.changeFindPageQueryWrapper(vo);
        if (!StringUtils.isEmpty(vo.getType())) {
            queryWrapper.eq("type", vo.getType());
        }
        return queryWrapper;
    }
}
